Properly format IPv6 addresses and return null for unknown addresses#14
Properly format IPv6 addresses and return null for unknown addresses#14clue merged 2 commits intoreactphp:masterfrom
Conversation
| if ($this->socket !== false) { | ||
| return stream_socket_get_name($this->socket, false); | ||
| } | ||
| return $this->sanitizeAddress(@stream_socket_get_name($this->socket, false)); |
There was a problem hiding this comment.
Is there a specific reason for omitting the $this->socket !== false now?
There was a problem hiding this comment.
There are several occasions where the stream_socket_get_name() call may emit a warning and unfortunately this is not reliable across different PHP versions and HHVM. Besides, the socket property can accessed from outside of this class so that the false check is simply not reliable either.
I've removed the check completely for consistency with the Socket component and simply discard any error messages from this call.
There was a problem hiding this comment.
Yes, my suggestion was not to remove @ from the stream_socket_get_name call again. I've been just curious if there are any consequences to potentially call stream_socket_get_name with false (or anything which isn't a resource).
There was a problem hiding this comment.
This is all covered by the test suite and we now make sure to either return the correct address string or null if it's unknown. Here's how this works under the hood:
>>> $f = stream_socket_client('google.com:80');
=> stream resource #279
>>> stream_socket_get_name($f, true);
=> "216.58.205.238:80"
>>> fclose($f);
=> true
>>> @stream_socket_get_name($f, true);
PHP warning: stream_socket_get_name(): supplied resource is not a valid stream resource on line 1
=> false
>>> @stream_socket_get_name(false, true);
PHP warning: stream_socket_get_name() expects parameter 1 to be resource, boolean given on line 1
=> falseThis uses a client socket for simplicity, the server socket emits similar errors but has far more error situations, e.g. the server socket does not have a remote address and returns false without emitting an error (unless you're on HHVM).
There was a problem hiding this comment.
Yes, i'm aware that stream_socket_get_namewould return false for non-resources, but we're relying now on PHP triggering a warning.
I also noticed, that i looks like being more inconsistent as before.
Note, that i'm reviewing without too much knowledge about the internals, so my comments are not meant to block a merge.
There was a problem hiding this comment.
No worries, I actually appreciate these kind of discussions :-)
This PR makes it so that we ignore any warnings (which are unreliable) and simply rely on the function returning false (which is documented behavior and works reliably across different PHP versions and HHVM). This is covered by both our test suite and the above gist.
Once this PR is in, the behavior is consistent throughout the Datagram component.
It is currently not consistent with regards to the Socket component. As you rightfully pointed out, the Socket component does currently not implement any checks and does not suppress any errors either. This means that this does in fact error out when one tries to access to remote address after the socket has been closed. See also reactphp/socket#19, for which I've prepared a PR that I'll file once reactphp/socket#61 is in 👍
There was a problem hiding this comment.
For the reference, here's the promised follow-up PR: reactphp/socket#63 👍
UDP6/IPv6 addresses should be returned as
[::1]:12345instead of the non-standard, ambiguous::1:12345.Refs reactphp/reactphp#199